

const http = require('http');
const path = require('path');
const url = require('url');
// const fsp = require('fs/promises');
const fs = require('fs');
const fsp = fs.promises;

const { createServer } = http;

// var server = createServer(function (req,res) { });
var server = createServer();

var siteUrl = path.join(__dirname, "../site"); // 默认站点路径 (前端相关的文件 放到site目录中)

server.on("request", function (req, res) {
    if (req.url != "/favicon.ico") {
        console.log("接收前端发送的请求");
        console.log(req.url);

        var urlInfo = url.parse(req.url, true);
        // console.log(urlInfo);

        var { pathname, query } = urlInfo;


        // 判断 req.url(路径) 在站点中是否存在   => 
        var readPath = path.join(siteUrl, pathname);
        if (fs.existsSync(readPath)) {  // 可以判断是否存在文件或文件夹
            // 如果访问的站点下的文件  -> 直接返回给前端
            // 如果访问的站点下的文件夹  -> 默认访问文件夹下的主页面

            fsp.stat(readPath).then(info => {
                if (info.isFile()) {  // 如果是文件
                    // 如果在站点中存在 -> 取的改文件 -> 传递给前端
                    var defaultEncoding = "utf-8";

                    var ext = path.extname(readPath);
                    console.log(11111, ext);

                    var types = {
                        ".html": "text/html;charset=utf-8",
                        ".css": "text/css;charset=utf-8",
                        ".js": "text/javascript;charset=utf-8",
                        ".json": "text/json;charset=utf-8",
                        ".jpg": "image/jpg",
                        ".jpeg": "image/jpeg",
                        ".png": "image/png",
                        ".gif": "image/gif",
                    }
                    // ext in types
                    if (types[ext]) {
                        res.writeHead(200, {
                            "Content-Type": types[ext],
                        })
                    }

                    var imgExts = [".jpg", ".jpeg", ".png", ".gif", ".webp"];
                    if (imgExts.includes(ext)) { //如果是图片格式
                        defaultEncoding = "binary";
                    }

                    // HTML css JS(!!)
                    fsp.readFile(readPath, defaultEncoding).then(txt => {
                        res.end(txt, defaultEncoding);
                    })
                } else if (info.isDirectory()) {
                    readPath = path.join(readPath, "./index.html");

                    // 如果在站点中存在 -> 取的改文件 -> 传递给前端
                    var defaultEncoding = "utf-8";

                    var ext = path.extname(readPath);
                    console.log(11111, ext);

                    var types = {
                        ".html": "text/html;charset=utf-8",
                        ".css": "text/css;charset=utf-8",
                        ".js": "text/javascript;charset=utf-8",
                        ".json": "text/json;charset=utf-8",
                        ".jpg": "image/jpg",
                        ".jpeg": "image/jpeg",
                        ".png": "image/png",
                        ".gif": "image/gif",
                    }
                    // ext in types
                    if (types[ext]) {
                        res.writeHead(200, {
                            "Content-Type": types[ext],
                        })
                    }

                    var imgExts = [".jpg", ".jpeg", ".png", ".gif", ".webp"];
                    if (imgExts.includes(ext)) { //如果是图片格式
                        defaultEncoding = "binary";
                    }

                    // HTML css JS(!!)
                    fsp.readFile(readPath, defaultEncoding).then(txt => {
                        res.end(txt, defaultEncoding);
                    })


                }
            })




        } else {
            res.end("2222");
        }




    } else {
        res.end();
    }
})


server.listen(3000, function () {
    console.log("服务器启动成功localhost:3000");
})

